using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GetTogether.Win.Utility
{
    public class FileHelper
    {
        public static void CopyDirectory(string from, string to)
        {
            System.Windows.Forms.MessageBox.Show(from + " To " + to);
            CopyDirectory(from, to, false);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="recurse"></param>
        public static void CopyDirectory(string from, string to, bool recurse)
        {
            DirectoryInfo fromDir = new DirectoryInfo(from);
            FileInfo[] files = fromDir.GetFiles();
            foreach (FileInfo file in files)
            {
                string copyFrom = PathCombine(from, file.Name);
                string copyTo = PathCombine(to, file.Name);
                File.Copy(copyFrom, copyTo);
            }
            if (recurse)
            {
                throw new NotImplementedException();
            }
        }

        public static string PathCombine(string path1, string path2)
        {
            if (string.IsNullOrEmpty(path2))
            {
                return path1;
            }

            // TODO handle UNCs. what if path2.StartsWith(@"\\")?
            if (path2[0] == Path.DirectorySeparatorChar || path2[0] == Path.AltDirectorySeparatorChar)
            {
                path2 = path2.Substring(1);
            }

            // Remove alternative directory separator characters, if necessary
            if (path1.IndexOf(Path.AltDirectorySeparatorChar) == -1)
            {
                path2 = path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            }

            // If path1 ends with a slash and path2 starts with a slash, remove one
            if (path1 != null)
            {
                char lastChar = path1[path1.Length - 1];

                if (lastChar == Path.DirectorySeparatorChar || lastChar == Path.AltDirectorySeparatorChar)
                {
                    while (path2.Length > 0)
                    {
                        if (path2[0] == lastChar)
                        {
                            path2 = path2.Substring(1);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return Path.Combine(path1, path2);
        }
    }
}
